package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.chenfeng.bo.CommentListBo;
import com.cskaoyan.bean.chenfeng.vo.CommentCountVo;
import com.cskaoyan.bean.chenfeng.vo.CommentListVo;
import com.cskaoyan.bean.leihao.po.CommentDeletePo;
import com.cskaoyan.bean.leihao.vo.Comment;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Auther: leihao
 * @Date: Created in 16:59 2022/6/28
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Value("${file.storage.origin}")
    String webAdress;

    @Autowired
    MarketCommentMapper commentMapper;

    @Override
    public CommonData<MarketComment> getCommentList(BasePageInfo basePageInfo, Integer userId, Integer valueId) {
        // 开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        // 创建example对象，添加准则
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        // 调用mapper查询数据库
        // List<Comment> comments = new ArrayList<>();
        List<MarketComment> list = commentMapper.selectByExample(commentExample);
        // for (int i = 0; i < list.size(); i++) {
        //     String picUrls = list.get(i).getPicUrls();
        //     Comment comment = new Comment();
        //     if (picUrls != null && !"[]".equals(picUrls)&&!"\"[]\"".equals(picUrls)) {
        //         String substring = picUrls.substring(2, picUrls.length() - 2);
        //         String[] split = substring.split(",");
        //         comment.setPicUrls(Arrays.asList(split));
        //     }else {
        //         comment.setPicUrls(new ArrayList<>());
        //     }
        //     comment.setId(list.get(i).getId());
        //     comment.setValueId(list.get(i).getValueId());
        //     comment.setType(list.get(i).getType());
        //     comment.setContent(list.get(i).getContent());
        //     comment.setAdminContent(list.get(i).getAdminContent());
        //     comment.setUserId(list.get(i).getUserId());
        //     comment.setHasPicture(list.get(i).getHasPicture());
        //     comment.setStar(list.get(i).getStar());
        //     comment.setAddTime(list.get(i).getAddTime());
        //     comment.setUpdateTime(list.get(i).getUpdateTime());
        //     comment.setDeleted(list.get(i).getDeleted());
        //     comments.add(comment);
        // }
        // 获取分页信息
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);

        // 封装数据返回结果
        return CommonData.data(pageInfo);
    }

    @Override
    public void delete(MarketComment marketComment) {
        Integer id = marketComment.getId();
        int i = commentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 统计指定商品指定评论类型的评论数
     *
     * @param valueId 商品的id
     * @param type    评论的类型
     * @return com.cskaoyan.bean.chenfeng.vo.CommentCountVo
     * @author Chen_Feng
     * @since 2022/06/29 20:16
     */
    @Override
    public CommentCountVo getCommentCount(Integer valueId, Integer type) {

        // 执行mapper操作
        Integer hasPicCount = commentMapper.selectCommentCountWithPicByValueIdAndType(valueId, type);
        Integer allCount = commentMapper.selectAllCommentCountByValueIdAndType(valueId, type);

        // 封装数据
        CommentCountVo data = new CommentCountVo();
        data.setAllCount(allCount);
        data.setHasPicCount(hasPicCount);

        return data;
    }

    /**
     * 获取指定商品的所有评论信息
     *
     * @param commentListBo 封装有请求信息的对象
     * @return com.cskaoyan.bean.param.CommonData<com.cskaoyan.bean.chenfeng.vo.CommentListVo>
     * @author Chen_Feng
     * @since 2022/06/29 20:42
     */
    @Override
    public CommonData<CommentListVo> getCommentListWithPage(CommentListBo commentListBo) {

        // 开启分页
        PageHelper.startPage(commentListBo.getPage(), commentListBo.getLimit());

        // 查询数据库
        Integer showType = commentListBo.getShowType();
        List<CommentListVo> list;
        if (showType != null && showType == 1) {
            list = commentMapper.selectCommentHasPicByValueIdAndTypeWithPage(commentListBo.getValueId(), commentListBo.getType());
        } else {
            list = commentMapper.selectAllCommentByValueIdAndTypeWithPage(commentListBo.getValueId(), commentListBo.getType());
        }
        // 处理图片路径
        for (CommentListVo commentListVo : list) {
            String[] picList = commentListVo.getPicList();
            for (int i = 0; i < picList.length; i++) {
                String s = picList[i];
                if (s != null && !s.startsWith("http://") && !s.startsWith("https://")) {
                    picList[i] = webAdress + s;
                }
            }
        }

        PageInfo<CommentListVo> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    /**
     * 发送评论
     *
     * @param marketComment 封装了评论信息
     * @return com.cskaoyan.bean.MarketComment
     * @author Chen_Feng
     * @since 2022/06/29 22:13
     */
    @Override
    public MarketComment PostComment(MarketComment marketComment) {

        // 补充信息
        Date date = new Date();
        marketComment.setDeleted(false);
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);

        // 图片路径处理
        String[] picUrls = marketComment.getPicUrls();
        String[] urls = new String[picUrls.length];
        for (int i = 0; i < picUrls.length; i++) {
            urls[i] = picUrls[i];
            picUrls[i] = urls[i].replace(webAdress, "");
        }

        commentMapper.insertSelective(marketComment);

        // 图片再处理
        marketComment.setPicUrls(urls);

        return marketComment;
    }
}
